﻿#include "b3mainwindow.h"
#include "ui_mainwindow.h"
#include "./DisplayWidget/DisplayWidget.h"
#include <QInputDialog>

#include "LampControl/B3PanelTestLamp.h"

B3MainWindow::B3MainWindow(QWidget *parent)
    :MainWindow(parent)
{
    QString path = configFilePath();
    QSettings settings( path + "/JHPhoto.ini", QSettings::IniFormat, this );

    m_recipeDockWidget = new RecipeDockWidget(this);
    m_recipeDockWidget->attachStageControl(m_pStageControl);
    m_recipeDockWidget->attachData(&m_measurementData);
    m_recipeDockWidget->attachCamera(m_pTisCamera);
    m_recipeDockWidget->hide();
    addDockWidget(Qt::LeftDockWidgetArea, m_recipeDockWidget);

    m_semiAutoMeasureDockWidget = new SemiAutoMeasureDockWidget(this);
    m_semiAutoMeasureDockWidget->attachStageControl(m_pStageControl);
    m_semiAutoMeasureDockWidget->attachData(&m_measurementData);
    m_semiAutoMeasureDockWidget->attachCamera(m_pTisCamera);
    addDockWidget(Qt::LeftDockWidgetArea, m_semiAutoMeasureDockWidget);
    m_semiAutoMeasureDockWidget->hide();

    setupSpecDockWidget();
    setupAutoFocus(settings);
    setupLampControl(settings);

    connect(ui->actionMoveToRelativePos, SIGNAL(triggered()), &m_MoveToRelativePos, SLOT(show()));
    connect(&m_MoveToRelativePos, SIGNAL(moveToRelPos(double, double, double)), m_pStageControl, SLOT(xyMoveToRelativePos(double,double,double)));
}

void B3MainWindow::setupLampControl(QSettings &settings)
{
    m_pTopLamp = new TopIlluminator(this);
    m_pBottomLamp = new BottomIlluminator(this);

    m_pTopLamp->loadSettings(settings);
    m_pBottomLamp->loadSettings(settings);

    m_pLampToolBar = new LampToolBar(this);
    m_pLampToolBar->attach(m_pTopLamp, m_pBottomLamp);
    addToolBar(Qt::LeftToolBarArea, m_pLampToolBar);
}

void B3MainWindow::setupAutoFocus(QSettings &settings)
{
    Q_UNUSED(settings);
    m_autoFocusThread = new AutoFocusThread(this);
    m_autoFocusThread->setMainWindow(this);
    m_autoFocusThread->setCameraImage(m_cameraImage);
    connect(m_pStageControlPad, SIGNAL(stopXY()), m_autoFocusThread, SLOT(stop()));
    connect(m_pStageControlPad, SIGNAL(stopZ()), m_autoFocusThread, SLOT(stop()));
    connect(m_autoFocusThread, SIGNAL(zMoveToPos(double,double)), m_pStageControl, SLOT(zMoveToPos(double,double)));
    connect(m_autoFocusThread, SIGNAL(zMoveToRelativePos(double,double)), m_pStageControl, SLOT(zMoveToRelativePos(double,double)));
    connect(m_pStageControl, SIGNAL(zMoveFinish()), m_autoFocusThread, SLOT(zMoveFinished()));
    connect(m_pStageControlPad, SIGNAL(autoFocus()), this, SLOT(autoFocus()));
}

void B3MainWindow::setupSpecDockWidget()
{
    connect(ui->actionNewDataBase, &QAction::triggered, this, &B3MainWindow::newDataBase);
    connect(ui->actionNewMeasurement, &QAction::triggered, this, &B3MainWindow::newMeasurement);
//    m_measureSpecDockWidget = new MeasureSpecDockWidget(this, this);
//    addDockWidget(Qt::LeftDockWidgetArea, m_measureSpecDockWidget);
//    m_measureSpecDockWidget->setVisible(false);
//    //connect(ui->actionNewDataBase, SIGNAL(triggered()), this, SLOT(on_actionNewDataBase_triggered()));
//    connect(m_measureSpecDockWidget, SIGNAL(goToXYZ(double,double,double,double)), m_pStageControl, SLOT(moveToPos(double,double,double,double)));
}

// 已读，在上下1mm范围内找最清晰的点
void B3MainWindow::autoFocus()
{
    if(m_autoFocusThread->isRunning())return;
    QList<double> zList;
    double zStep = 0;
    const double step[] = {0.1, 0.1, 0.1, 0.05, 0.05, 0.05};
    zStep = step[m_lens];

    for(int i = -10; i <= 10; i++)
    {
        zList << m_theoryPos_mm[2] + i * zStep;
    }
    m_autoFocusThread->setZPositions(zList);
    m_autoFocusThread->start();// 自动对焦之后会停止相机，这时显示最清晰的图像，但是z位置不是最佳位置
}

void B3MainWindow::newDataBase()
{
    m_recipeDockWidget->show();
    m_semiAutoMeasureDockWidget->hide();
}

void B3MainWindow::newMeasurement()
{
    m_recipeDockWidget->hide();
    m_semiAutoMeasureDockWidget->show();
}

void B3MainWindow::displayMeasureDialog(QString DataBaseName, QString SqlDirPath)
{
    if(!m_pTisCamera->isValid())
    {
        QMessageBox::information(this, QStringLiteral("Error"), QStringLiteral("camera not open!") );
        //return;
    }
    m_selectDataBase->close();
//    m_dataMeasurement = new DataMeasurementDialog(this, this, m_cameraImage);
//    m_dataMeasurement->setMainWindow(this);
//    m_dataMeasurement->setStageControl(m_pStageControl);
//    m_dataMeasurement->setData(DataBaseName, SqlDirPath);
    m_isMeasure = true;
//    m_dataMeasurement->exec();
    m_isMeasure = false;
 //   delete m_dataMeasurement;
}
